package com.zhongxi.guoyuan.fragment.clearweeddetail;


import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.zhongxi.guoyuan.OffLineShop.OffLineBaseFragment;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.projectItem.ProjectDetailResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.UiUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClearweedDetailFragment extends OffLineBaseFragment {


    @Bind(R.id.clearweed_detail_webview)
    WebView mClearweedDetailWebview;

    public ClearweedDetailFragment() {

    }

    @Override
    protected View init() {
        View mView = UiUtils.inflate(R.layout.fragment_clearweed_detail);
        ButterKnife.bind(this, mView);
        return mView;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mClearweedDetailWebview.getSettings().setJavaScriptEnabled(true);
        mClearweedDetailWebview.getSettings().setBuiltInZoomControls(false);
        mClearweedDetailWebview.getSettings().setDisplayZoomControls(false);
        mClearweedDetailWebview.getSettings().setDefaultTextEncodingName("UTF-8");
        mClearweedDetailWebview.getSettings().setBlockNetworkImage(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mClearweedDetailWebview.getSettings().setMixedContentMode(mClearweedDetailWebview.getSettings().MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mClearweedDetailWebview.removeAllViews();
        ButterKnife.unbind(this);
    }

    private String getNewContent(String htmltext) {

        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "100%").attr("height", "auto");
        }

        return doc.toString();
    }

    public void ResultData(String desc) {
        try {
            if (null != desc && !"".equals(desc)) {
                    mClearweedDetailWebview.loadDataWithBaseURL(null, getNewContent(desc), "text/html", "utf-8", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
